package com.acc.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.acc.entity.User;
import com.acc.service.HelloService;
import com.acc.utils.Md5Utils;
/**
 * 
 * @author 杨天然
 *
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	private HelloService helloService= new HelloService();
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//实现用户的增加
		String userName = request.getParameter("username");
		String pwd = request.getParameter("password");
		String idcard = request.getParameter("idCard");
		String password = null;
		if(pwd != null) {
			password = Md5Utils.md5(pwd); 
		}
		Integer gender = null;
		
		Integer type = null;
		try {
			gender = Integer.parseInt(request.getParameter("gender"));
			type = Integer.parseInt(request.getParameter("type"));
		}catch(NumberFormatException nbf) {
			nbf.printStackTrace();
		}
		
		User customer = new User();
		customer.setUserName(userName);
		customer.setPassword(password);
		customer.setGender(gender);
		customer.setIdcard(idcard);
		
		customer.setType(type);
		HttpSession session = request.getSession();
		helloService.add(customer);
		User user = (User) session.getAttribute("user");
		response.sendRedirect("index.jsp");

	}

}
